package band.wukong.practice.webservice.kh.sample12.nongen;

import band.wukong.practice.webservice.kh.sample12.RobotService;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.testng.Assert;
import org.testng.annotations.Test;

import javax.xml.ws.soap.SOAPFaultException;

/**
 * @author wukong(wukonggg@139.com)
 */
public class ClientTest {

    @Test
    public void sayHi4StringInInterceptor() {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress(RobotConst.SERVICE_URL);
        factory.setServiceClass(RobotService.class);

        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.getOutInterceptors().add(new ClientOutboundStringLicenseInterceptor());

        RobotService port = (RobotService) factory.create();
        String echo = port.sayHi("CXF");
        Assert.assertEquals(echo, "Hi, CXF! ");
    }

    @Test
    public void sayHi4ObjectInInterceptor() {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress(RobotConst.SERVICE_URL);
        factory.setServiceClass(RobotService.class);

        factory.getOutInterceptors().add(new LoggingOutInterceptor());

        factory.getOutInterceptors().add(new ClientOutboundObjectLicenseInterceptor(
                new License("admin", "666")));

        RobotService port = (RobotService) factory.create();
        String echo = port.sayHi("CXF");
        Assert.assertEquals(echo, "Hi, CXF! ");
    }

    @Test(expectedExceptions = SOAPFaultException.class)
    public void sayHi4ObjectInInterceptorFailed() {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress(RobotConst.SERVICE_URL);
        factory.setServiceClass(RobotService.class);

        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.getInInterceptors().add(new LoggingInInterceptor());

        factory.getOutInterceptors().add(new ClientOutboundObjectLicenseInterceptor(
                new License("ozzy", "123")));

        RobotService port = (RobotService) factory.create();
        String echo = port.sayHi("CXF");
        Assert.assertEquals(echo, "Hi, CXF! ");
    }
}
